package com.yinsw.controller;

import com.alibaba.fastjson.JSONObject;
import com.yinsw.config.OssUtils;
import com.yinsw.entity.HouseEntity;
import com.yinsw.entity.TbRegion;
import com.yinsw.entity.TbUser;
import com.yinsw.redis.RedisService;
import com.yinsw.result.Result;
import com.yinsw.service.HouseService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author printguy
 * @version 1.0.0
 * @className HouseController.java
 * @description TODO
 * @createTime 2022年07月18日 14:46:00
 */
@Controller
@RequestMapping("system/house")
public class HouseController {

    private final HouseService houseService;
    private final RedisService redisService;
    private final RestHighLevelClient client;
    private final OssUtils ossUtils;

    public HouseController(HouseService houseService, RedisService redisService, RestHighLevelClient client, OssUtils ossUtils) {
        this.houseService = houseService;
        this.redisService = redisService;
        this.client = client;
        this.ossUtils = ossUtils;
    }
    @RequestMapping("/")
    public String index() {
        return "index";
    }

    //二手房出售详情
    @RequestMapping("/esfSaleDetail/{id}")
    public String esfSaleDetail(@PathVariable("id") Long id, Model model) {
        HouseEntity houseEntity = null;
        SearchRequest searchRequest = new SearchRequest("house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("id",id));
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = searchResponse.getHits();
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                houseEntity = JSONObject.parseObject(sourceAsString, HouseEntity.class);
            }
        } catch (IOException e) {

            throw new RuntimeException(e);
        }
        model.addAttribute("detail",houseEntity);
        return "esfSaleDetail";
    }

    //二手房出售列表
    @RequestMapping("/esfSale")
    public String esfSale() {
        return "esfSale";
    }

    //二手房出租详情
    @RequestMapping("/esfRentDetail/{id}")
    public String esfRentDetail(@PathVariable("id") Long id,Model model){
        HouseEntity houseEntity = null;
        SearchRequest searchRequest = new SearchRequest("house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("id",id));
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse resp = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = resp.getHits();
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                houseEntity = JSONObject.parseObject(sourceAsString, HouseEntity.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("detail",houseEntity);
        return "esfRentDetail";
    }

    //二手房出租列表
    @RequestMapping("/esfRent")
    public String esfRent() {
        return "esfRent";
    }

    //写字楼出售详情
    @RequestMapping("/xzlSaleDetail/{id}")
    public String xzlSaleDetail(@PathVariable("id")Long id,Model model){
        HouseEntity houseEntity = null;
        SearchRequest searchRequest = new SearchRequest("house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("id",id));
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse resp = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = resp.getHits();
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                houseEntity = JSONObject.parseObject(sourceAsString, HouseEntity.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("detail",houseEntity);
        return "xzlSaleDetail";
    }

    //写字楼出售列表
    @RequestMapping("/xzlSale")
    public String xzlSale() {
        return "xzlSale";
    }

    //写字楼出租详情
    @RequestMapping("/xzlRentDetail/{id}")
    public String xzlRent(@PathVariable("id") Long id,Model model){
        HouseEntity houseEntity = null;
        SearchRequest searchRequest = new SearchRequest("house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("id",id));
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse resp = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = resp.getHits();
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                houseEntity = JSONObject.parseObject(sourceAsString, HouseEntity.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("detail",houseEntity);
        return "xzlRentDetail";
    }

    //写字楼出租列表
    @RequestMapping("/xzlRent")
    public String xzlRent() {
        return "xzlRent";
    }

    //商铺出售详情
    @RequestMapping("/spSaleDetail/{id}")
    public String spSaleDetail(@PathVariable("id")Long id,Model model){
        HouseEntity houseEntity = null;
        SearchRequest searchRequest = new SearchRequest("house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("id",id));
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse resp = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = resp.getHits();
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                houseEntity = JSONObject.parseObject(sourceAsString, HouseEntity.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("detail",houseEntity);
        return "spSaleDetail";
    }

    //商铺出售列表
    @RequestMapping("/spSale")
    public String spSale() {
        return "spSale";
    }

    //商铺出租详情
    @RequestMapping("/spRentDetail/{id}")
    public String spRentDetail(@PathVariable("id") Long id,Model model){
        HouseEntity houseEntity = null;
        SearchRequest searchRequest = new SearchRequest("house");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        boolQueryBuilder.must(QueryBuilders.termQuery("id",id));
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        try {
            SearchResponse resp = client.search(searchRequest, RequestOptions.DEFAULT);
            SearchHits hits = resp.getHits();
            for (SearchHit hit : hits.getHits()) {
                String sourceAsString = hit.getSourceAsString();
                houseEntity = JSONObject.parseObject(sourceAsString, HouseEntity.class);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        model.addAttribute("detail",houseEntity);
        return "spRentDetail";
    }

    //商铺出租列表
    @RequestMapping("/spRent")
    public String spRent() {
        return "spRent";
    }

    //es搜索
    @RequestMapping("/search")
    @ResponseBody
    public Result<String> index(@RequestParam Map<String,Object> map) {
        String title = (String) map.get("title");
        return Result.success(title);
    }

    @RequestMapping("/esfSaleList")
    @ResponseBody
    public Result<List<HouseEntity>> list(@RequestParam Map<String,Object> map) {
        List<HouseEntity> houseList = houseService.list(map);
        return Result.success(houseList);
    }

    //跳转至出售住宅页面
    @RequestMapping("/gozzsell")
    public String gozzsell(){
        return "zzsell";
    }

    //省下拉框
    @RequestMapping("/provinceList")
    @ResponseBody
    public List<TbRegion> provinceList(){
        return houseService.provinceList();
    }

    //市下拉框
    @RequestMapping("/cityList")
    @ResponseBody
    public List<TbRegion> cityList(@RequestParam Integer id){
        return houseService.cityList(id);
    }

    //县下拉框
    @RequestMapping("/countyList")
    @ResponseBody
    public List<TbRegion> countyList(@RequestParam Integer id){
        return houseService.countyList(id);
    }

    //添加房源
    @RequestMapping("/save")
    @ResponseBody
    public int save(HouseEntity houseEntity){
        return houseService.save(houseEntity);
    }

    //上传房源图片
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam MultipartFile file){
        Result r = ossUtils.uploadFile(file);
        System.out.println(r);
        return r;
    }

    //上传身份证人像面图片
    @RequestMapping("/upload2")
    @ResponseBody
    public Result upload2(@RequestParam MultipartFile idCardImgFile){
        Result r = ossUtils.uploadFile(idCardImgFile);
        System.out.println(r);
        return r;
    }

    //上传房产证图片
    @RequestMapping("/upload3")
    @ResponseBody
    public Result upload3(@RequestParam MultipartFile houseCardImgFile){
        Result r = ossUtils.uploadFile(houseCardImgFile);
        System.out.println(r);
        return r;
    }

    @RequestMapping("/goLogin")
    public String goLogin(){
        return "login";
    }

    //登录
    @RequestMapping("/login")
    @ResponseBody
    public int login(TbUser tbUser, HttpServletResponse response){
        TbUser user = houseService.getUser(tbUser.getName());
        if (user!=null){
            if (user.getPwd().equals(tbUser.getPwd())){
                redisService.redisTemplate.opsForValue().set("user", JSONObject.toJSONString(user));
                Cookie cookie = new Cookie("userName", user.getName());
                cookie.setPath("/");
                response.addCookie(cookie);
                return 1;
            }
        }
        return 0;
    }

    //跳转到写字楼出售页面
    @RequestMapping("/goxzlsell")
    public String goxzlsell(){
        return "xzlsell";
    }

    //跳转到商铺出售页面
    @RequestMapping("/gospsell")
    public String gospsell(){
        return "spsell";
    }

    //跳转到房贷计算器
    @RequestMapping("/goJisuanqi")
    public String gojsq(){
        return "jsq";
    }

    @RequestMapping("/goIndex1")
    public String goIndex1(){
        return "index1";
    }

}
